package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/12/20 19:16
 * @Version 1.0
 */
@RequestMapping("/api/cart")
@RestController
public class CartApiController {

    @Autowired
    CartService cartService;

    @GetMapping("/cartList")
    public Result getCartList(){

        List<CartInfo> cartInfos = cartService.displayItems();

//        String cartKey = cartService.determinCartKey();
//
//        List<CartInfo> cartInfos = cartService.getCartItems(cartKey);

        return Result.ok(cartInfos);
    }

    @PostMapping("/addToCart/{skuId}/{num}")
    public Result updateItemNum(@PathVariable("skuId") Long skuId,
                                @PathVariable("num") Integer num){

        String cartKey = cartService.determinCartKey();

        cartService.updateItemNum(cartKey,skuId,num);

        return Result.ok();
    }

    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result checkItem(@PathVariable("skuId") Long skuId,
                            @PathVariable("checked") Integer checked){

        String cartKey = cartService.determinCartKey();

        cartService.checkItem(cartKey,skuId,checked);

        return Result.ok();
    }

    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId){

        String cartKey = cartService.determinCartKey();

        cartService.deleteItem(cartKey,skuId);

        return Result.ok();
    }

}
